/* Performing functions of list in python by using arrays in C.
6-3-2011        */


#include<stdio.h>

int *array;

/* Creating a list */

create(int n)
{
       array=malloc(n*sizeof(int));
       int i;
       for(i=0;i<n;i++)
       {
            printf("ENTER THE NUMBER:");
            scanf("%d",&array[i]);
       } 
             
}

/* Displaying the list */

display(int n)
{
            if(n==0)
            {
                    printf("THE LIST IS EMPTY,BEFORE EXPERIMENTING CREATE A LIST");
            }
            else
            {
                    int i;
                    for(i=0;i<n;i++)
                    {
                                    printf("%d ",array[i]);       
                    }
            }               
}


                      
/* Append an element at last  */

append(int n)
{
           printf("ENTER THE NUMBER YOU WANT TO APPEND:");
           scanf("%d",&array[n-1]);
        
}

/*Inserting an element at a particular location */

insert(int n)
{
           int i,j,num,p,l=n;
           printf("ENTER A LOCATION BETWEEN 0 to %d:",l-1);
           scanf("%d",&j);
           printf("ENTER THE NUMBER TO BE INSERTED:");
           scanf("%d",&num);
           for(i=0;i<n;i++)
           {
                 if(i==j)
                    {
                      for(p=n-2;p>=i;p--)
                         {
                           array[p+1]=array[p];
                         }
                      array[i]=num;                                  
                    }
           }                                 
}


/* Indexing a node*/

index(int n)
{
           int i,num,count=0;
           printf("ENTER A NUMBER FROM THE LIST");
           scanf("%d",&num);
           for(i=0;i<n;i++)
           {
                           if(array[i]==num)
                           {
                                            printf("THE NUMBER %d IS AT INDEX %d",num,i);
                                            count++;  
                           }
           }
           if(count)
           {
                             printf("HENCE THE NUMBER %d OCCURS %d TIMES IN THE LIST",num,count);
           }
           else
           {
                             printf("NUMBER NOT FOUND IN THE LIST");
           }
           
}

/* Extending a list  */

extend(int n,int i)
{
           int j,d;
           d=n-i;
           for(j=d;j<n;j++)
           {
                           printf("ENTER THE NUMBER:");
                           scanf("%d",&array[j]);
           }
}  


/* Removes a no. in a list  */

remove_num(int n)
{
           int i,p,num;
           printf("ENTER A NUMBER FROM THE LIST");
           scanf("%d",&num);
           for(i=0;i<n;i++)
           {
                           if(array[i]==num)
                           {
                                   for(p=i;p<n;p++)
                                   {
                                                   array[p]=array[p+1];
                                   }
                                   return;                                                              
                           }
           }          
}
/* Counts the occurances of a no. */

count_occurence(int n)
{
                    int i,num,count=0;
                    printf("ENTER A NUMBER FROM THE LISTt");
                    scanf("%d",&num);
                    for(i=0;i<n;i++)
                    {
                                    if(array[i]==num)
                                    {
                                                     count++;
                                    }
                    }
                    if(count)
                    {
                             printf("THE NUMBER %d OCCURS %d TIMES IN THE LIST",num,count);
                    }
                    else
                    {
                             printf("THE NUMBER %d OCCURS 0 TIMES IN THE LIST,HENCE NUMBER NOT FOUND IN THE LIST",num);
                    }
                    return;
                                                     
}
/* Sorting a list */

sort(int n)
{
         int i,j,low,temp;
         
         for(i=0;i<n;i++)
         {
                         low=array[i];
                         for(j=i+1;j<n;j++)
                         {
                                         
                                         if(low>array[j])
                                         {
                                                              temp=low;
                                                              low=array[j];
                                                              array[j]=temp;                                                              
                                                              
                                         }
                                         
                                         
                                         
                         }
                         array[i]=low;
                         
                       
         
         }
}
         

/* Reversing a list */

reverse(int n)
{
            int i,temp,p=n;
            if(n%2==0)
            {
                      for(i=0;i<=(p/2);i++)
                      {
                                          temp=array[i];
                                          array[i]=array[p-1];
                                          array[p-1]=temp;
                                          p--;
                      }
            }
            else
            {
                      for(i=0;i<p;i++)
                      {
                                          temp=array[i];
                                          array[i]=array[p-1];
                                          array[p-1]=temp;
                                          p--;
                      }
            }
                     
}



/*Popping a node */

pop(int n)
{
           int i,j,p,l=n;
           printf("ENTER A LOCATION 0 to %d ,ENTER A VALUE GREATER THAN OR EQUAL TO %d FOR DEFAULT POP(POPS THE LAST ELEMENT):",l-1,l);
           scanf("%d",&j);
           if(j<l)
           {
                  for(i=0;i<n;i++)
                  {
                           if(i==j)
                           {
                                   for(p=i;p<n;p++)
                                   {
                                                   array[p]=array[p+1];
                                   }                                
                                 
                           }
                  }
           }
           else
           {
                  return;
           }           
}








main()
{
      int i;
      int n;
      
      while(1) 
      {
               printf("\n\n\n\n1.TO CREATE A NEW LIST \n");
               printf("2.TO APPEND :\n");
               printf("3.TO EXTEND THE LIST:\n");
               printf("4.TO INSERT A NUMBER AT A PARTICULAR LOCATION IN THE lIST:\n");
               printf("5.TO REMOVE A NUMBER FROM THE LIST:\n");
               printf("6.TO COUNT THE OCCURRENCE OF A GIVEN NUMBER IN THE LIST:\n");
               printf("7.TO SORT THE LIST:\n");
               printf("8.TO REVERSE THE LIST :\n");
               printf("9.TO PRINT THE LENGTH OF THE LIST:\n");
               printf("10.TO DISPLAY THE ELEMENTS IN THE LIST:\n");
               printf("11.TO POP A  ELEMENT FROM THE LIST:\n");
               printf("12.TO PRINT THE INDEX OF GIVEN NUMBER IN THE LIST:\n");
               printf("13.TO EXIT FROM THE PROGRAM:\n");
               
               printf("PLEASE,ENTER YOUR CHOICE:\n");
               scanf("%d",&i); 

               switch(i)
               {
                       case 1:
                            {
                                       printf("HOW MANY ELEMENTS YOU WANT IN THE LIST:");
                                       scanf("%d",&n);
                                       create(n);
                                       break;
                                       
                            }
                       case 2:
                            {
                                       n++;
                                       void *realloc(int *array, int n);
                                       append(n);
                                       break;
                            }
                       case 3:
                            {
                                       int i;
                                       printf("BY HOW MANY ELEMENTS YOU WANT TO EXTEND THE LIST:");
                                       scanf("%d",&i);
                                       n=n+i;                                       
                                       void *realloc(int *array, int n);                                                                             
                                       extend(n,i);
                                       break;                                       
                            }  
                       case 4:
                            {
                                       n++;
                                       void *realloc(int *array, int n);
                                       insert(n);                                      
                                       break;           
                            }
                       case 5:
                            {
                                       remove_num(n);
                                       n--;
                                       void *realloc(int *array,int n);                                     
                                       break;                                       
                            }
                       case 6:
                            {
                                       count_occurence(n);                                       
                                       break;                                       
                            }
                       case 7:
                            {
                                       sort(n);
                                       display(n);                                       
                                       break;                                       
                            }
                       case 8:
                            {
                                       reverse(n);
                                       display(n);                                       
                                       break;                                       
                            }
                       case 9:
                            {
                                       printf("THERE ARE %d ELEMENTS IN THE LIST",n);                                       
                                       break;                                       
                            }
                       case 10:
                            {
                                       display(n); 
                                       break;                                       
                            }
                       case 11:
                            {
                                       pop(n);
                                       n--;
                                       void *realloc(int *array,int n);                                     
                                       break;                                                                             
                            }
                       case 12:
                            {
                                       index(n);                                      
                                       break;                                                                               
                            }
                       case 13:
                            {
                                       exit(0);                                       
                            }
                       
               }
      }
}
